#coding:utf-8
from locust import  TaskSet, task,HttpUser
import time,os,random
accesstoken={"access_token":"3183cd54ad64f71e6f818a2007f7b338"}

class GetData(TaskSet):

    # 用户执行task前调用
    def on_start(self):
        print('this is on start')

    # 用户执行完task后调用
    def on_stop(self):
        print('this is on stop')

    @task(2)
    def get_token(self):
        # 头部
        header = {'app_key':'1EPl7Uk41gMdZEbXUJ5P', '_method_name':'post'}
        # 参数
        data = {'request_data':'{"app_secret":"1mUES1HEQHH5Uc1Qsmp0"}'}
        # 发起请求
        response = self.client.post('/oauth/token',  headers=header,data=data)
        print(response.text)
    @task(1)
    def get_gettermopen(self):
        datas = {'request_data':'{"term_code":6888888}'}
        response = self.client.post('/faceapi/gettermopen',headers = accesstoken,data=datas)
        print('gettermopen:',response.text)


class UserImitate(HttpUser):
    # 域名/IP
    host = 'http://192.168.112.144:8080/aiface'
    # 设置task
    tasks = [GetData]
    # 指定在执行任务(min_wait和max_wait)和其他用户行为之间的最小和最大等待时间(以毫秒为单位—每个模拟用户)
    min_wait = 5000
    max_wait = 9000
if __name__ == "__main__":
    cmd = 'locust -f cmdlocust.py'
    os.system(cmd)